package com.qf.controller;

import com.qf.pojo.BaseResp;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.utils.UploadToken;
import com.qf.utils.UploadUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
@RequestMapping("/login")
    public BaseResp login(@RequestBody User user){
        return userService.login(user);
    }

    @RequestMapping("/add")
    public BaseResp add(@RequestBody User user){
        return userService.add(user);
    }

    @RequestMapping("/del")
    public BaseResp del(@RequestBody Map map) {
        return userService.del((Integer) map.get("id"));
    }

    @RequestMapping("/findAll")
    public BaseResp findAll(@RequestBody Map map){
        return userService.findAll(Integer.valueOf(map.get("page").toString()),Integer.valueOf(map.get("size").toString()));
    }

    @PostMapping("/upload")
    public BaseResp upload(@RequestParam("file") MultipartFile multipartFile){
        String upload = UploadUtils.upload(multipartFile);
        // System.out.println(map);
        BaseResp baseResp = new BaseResp();
        baseResp.setData(upload);
        return baseResp;
    }

    @GetMapping("/gettoken")
    public BaseResp gettoken(){
        String token = UploadToken.upload();
        BaseResp baseResp = new BaseResp();
        baseResp.setData(token);
        return baseResp;
    }
    @RequestMapping("/logout")
    public BaseResp logout(){
        Subject subject = SecurityUtils.getSubject();
        BaseResp baseResp = new BaseResp();
        try {
            subject.logout();
            baseResp.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseResp;
    }
}
